Completed
Push — master ( 7cdd5a...e5393a )
by greg
02:22
created

post-upload.js ➔ ... ➔ req.busboy.finish   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
import path from 'path'
2
import fse from 'fs-extra'
3
import mkdirp from 'mkdirp'
4
import limax from 'limax'
5
6
import {
7
  config,
8
  abeExtend
9
} from '../../cli'
10
11
var route = function(req, res, next){
12
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
13
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
15
  var resp = {success: 1}
16
  var filePath
17
  var folderWebPath = '/' + config.upload.image
18
  folderWebPath = abeExtend.hooks.instance.trigger('beforeSaveImage', folderWebPath, req)
19
  var folderFilePath = path.join(config.root, config.publish.url, folderWebPath)
20
  mkdirp.sync(folderFilePath)
21
  req.pipe(req.busboy)
22
  req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
23
    var ext = path.extname(filename)
24
    var filenameNoExt = path.basename(filename,ext)
25
    var randID = '-' + (((1+Math.random())*0x100000)|0).toString(16).substring(2)
26
    var slug = limax(filenameNoExt, {separateNumbers: false}) + randID + ext
27
    var hasSentHeader = false
28
    
29
    filePath = path.join(folderFilePath, slug)
30
    resp['filePath'] = path.join(folderWebPath, slug)
31
32
    var returnErr = function (msg) {
33
      hasSentHeader = true
34
      file.resume()
35
      res.set('Content-Type', 'application/json')
36
      res.send(JSON.stringify({error: 1, response: msg}))
37
    }
38
39
    file.on('limit', function() {
40
      returnErr('file is too big')
41
    })
42
43
    if (mimetype !== 'image/jpeg' && mimetype !== 'image/png' && mimetype !== 'image/svg+xml' && mimetype !== 'video/mp4') {
44
      returnErr('unauthorized file')
45
    } else if (ext !== '.jpg' && ext !== '.jpeg' && ext !== '.png' && ext !== '.svg' && ext !== '.mp4') {
46
      returnErr('not a valid asset')
47
    }
48
49
    var fstream = fse.createWriteStream(filePath)
50
51
    fstream.on('finish', function() {
52
      if(hasSentHeader) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
      if(/\.(jpg|png|gif|svg)/.test(filePath)){
54
        resp = abeExtend.hooks.instance.trigger('afterSaveImage', resp, req)
55
      }
56
        res.set('Content-Type', 'application/json')
57
        res.send(JSON.stringify(resp))
58
    })
59
60
    file.pipe(fstream)
61
  })
62
}
63
64
export default route